package com.sui.bigdata.sml.web.repository.mapper;

import com.sui.bigdata.sml.web.repository.model.Model;
import com.sui.bigdata.sml.web.repository.model.ModelGroup;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author songhaicheng
 * @date 2020/1/15 10:01
 * @description
 * @reviewer
 */
@Repository
public interface ModelGroupMapper {

    @Insert("insert into model_group(serving_token,model_tokens,user_id,active,name,description) values(#{servingToken},#{modelTokens},#{userId},#{active},#{name},#{description})")
    @Options(useGeneratedKeys = true)
    int insert(ModelGroup group);

    @Select("select * from model_group where id=#{id}")
    ModelGroup get(Long id);

    @Select("select * from model_group where serving_token=#{servingToken}")
    ModelGroup getByServingToken(String servingToken);

    @Select("<script>" +
            "select * from model_group" +
            "<if test='userId!=null'>" +
            " where user_id=#{userId}" +
            "</if>" +
            "</script>")
    List<ModelGroup> listByUserId(@Param("userId") Long userId);

    @Select("<script>" +
            "select * from model_group where model_tokens like concat('%',#{token},'%')" +
            "<if test='active!=null'>" +
            " and active=#{active}" +
            "</if>" +
            "</script>")
    List<ModelGroup> listHavingModelToken(@Param("token") String token, @Param("active") Boolean active);

    @Update("<script>" +
            "update model_group set model_tokens=#{modelTokens}" +
            "<if test='active!=null'>" +
            ",active=#{active}" +
            "</if>" +
            " where serving_token=#{servingToken}" +
            "</script>")
    int update(ModelGroup group);

}
